local wen__cuihua = fk.CreateSkill {
  name = "wen__cuihua",
}

Fk:loadTranslationTable{
  ["wen__cuihua"] = "催化",
  [":wen__cuihua"] = "你可以将手牌中或场上的一张【闪电】当任意一张智囊牌使用",
  ["#wen__cuihua"] = "催化：选择一名角色", 
  ["#wen__cuihua-dismantlement"] = "催化：选择【过河拆桥】的目标角色",  
}

wen__cuihua:addEffect("viewas", {
  pattern = "dismantlement,ex_nihilo,nullification",
  prompt = "#wen__cuihua",
  interaction = function(self, player)
    local all_names = {"dismantlement", "ex_nihilo", "nullification"}
    local names = player:getViewAsCardNames(wen__cuihua.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards, c, extra_data)
    if #selected == 0 then
        if to_select == player then
            local has_lightning = false
            for _, id in ipairs(to_select:getCardIds("hj")) do
                if Fk:getCardById(id).name == "lightning" then
                    has_lightning = true
                    break
                end
            end
            return has_lightning
        else
            local has_lightning_in_judge = false
            for _, id in ipairs(to_select:getCardIds("j")) do
                if Fk:getCardById(id).name == "lightning" then
                    has_lightning_in_judge = true
                    break
                end
            end
            return has_lightning_in_judge
        end
    end
  end,
  
  feasible = function(self, player, selected, selected_cards, card)
    return #selected == 1
  end,

  on_use = function(self, room, cardUseEvent, _, params)
    local player = cardUseEvent.from
    local to = cardUseEvent.tos[1]
    local card = Fk:cloneCard(self.interaction.data)
    local lightnings = {}
    if player == to then
      lightnings = table.filter(to:getCardIds("hj"), function(id)
        local card_id = Fk:getCardById(id)
        return card_id and card_id.trueName == "lightning"
      end)
    else
      lightnings = table.filter(to:getCardIds("j"), function(id)
        local card_id = Fk:getCardById(id)
        return card_id and card_id.trueName == "lightning"
      end)
    end
    
    local lightning
    if #lightnings == 0 then
      return
    elseif #lightnings == 1 then
      lightning = lightnings[1]
    else
      lightning = room:askToChooseCard(player, {
        target = to,
        flag = {
          card_data = {
            { "天气瓶", lightnings }
          }
        },
        skill_name = wen__cuihua.name
      })
    end

    if not lightning then
      return
    end
    
    if room:getCardArea(lightning) ~= Player.Hand then
    room:obtainCard(player, lightning, true, fk.ReasonPrey, player, wen__cuihua.name)
    end
      
      if room:getCardOwner(lightning) == player and room:getCardArea(lightning) == Player.Hand then
        card.skillName = wen__cuihua.name
        card:addSubcard(lightning)

        local use = {
          from = player,
          tos = {},
          card = card,
        }
        
        if self.interaction.data == "dismantlement" then
          local new_use = room:askToUseVirtualCard(player, {
            name = "dismantlement",
            subcards = { lightning },
            prompt = "#wen__cuihua-dismantlement",
            skill_name = wen__cuihua.name,
            extra_data = params and params.extra_data,
            cancelable = false,
            skip = true
          })
          if new_use then
            use.tos = new_use.tos
          else
            return
          end
        end
        return use
      end
  end,
})

return wen__cuihua